// MiriamDial01.cpp: Implementierung der Klasse MiriamDial01.
//
//////////////////////////////////////////////////////////////////////

#include "MiriamDial01.h"

//#include "MiriamColors.h"

//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////

MiriamDial01::MiriamDial01(QWidget* parent):QWidget(parent)
{
//	const QString fileName("C:\\Temp\\MiriamDial01.png");
//	if(QFile::exists(fileName))
//		baseImage = QImage(fileName);
	m_angle = 0;
}

MiriamDial01::~MiriamDial01()
{

}
void MiriamDial01::setAngle(const int angle)
{
	m_angle = (angle+90)%360;
}
void MiriamDial01::paintEvent(QPaintEvent *event)
{
/*
//could just take an image
	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	painter.drawImage ( QPoint(0,0), baseImage );//re-draw the image
	
	return;
/**/
//but... could do this too...

	//if the thing won't be resized, I can make these lines and paths in the constructor - not very general though
	int side = qMin(width(), height());
	int spacing = side/20;

	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);	

	//draw the bounding circle, filled with the background colour
	painter.setPen(QPen(m_backgroundColor, 1, Qt::SolidLine));
	painter.setBrush ( m_backgroundColor );
	const QRect boundingRectangle(0, 0, side, side);
	painter.drawEllipse(boundingRectangle);

	//draw the outer circle
    painter.setPen(QPen(palette().color(QPalette::WindowText), 1, Qt::SolidLine));
	painter.setBrush ( m_middleColor );
	const QRect outerRectangle(spacing, spacing, side-spacing*2, side-spacing*2);
	painter.drawEllipse(outerRectangle);

	//now draw the lines
	QVector<QLine> lines;
	lines.append(QLine(0,0,side, side));//topleft to bottomright
	lines.append(QLine(side/2,0,side/2, side));//topcenter to bottom center
	lines.append(QLine(side,0,0,side));//topright to bottom left
	lines.append(QLine(side,side/2,0,side/2));
	painter.setPen(QPen(palette().color(QPalette::WindowText), 1, Qt::SolidLine));
	painter.setBrush ( palette().color(QPalette::WindowText) );
	painter.drawLines(lines);

	//draw the inner circle, filled with the background colour
	painter.setPen(QPen(palette().color(QPalette::WindowText), 1, Qt::SolidLine));
	painter.setBrush ( m_middleColor );
	const QRect innerRectangle(spacing*2, spacing*2, side-spacing*4, side-spacing*4);
	painter.drawEllipse(innerRectangle);

	//draw the arrow markers - we need to integrate the angle 
	//every data point times the time to the next data point plus the current integral
	bool m_drawMarkers = true;
	if(m_drawMarkers){
		int markerScale = spacing*2.5;
		
		QVector<QLine> triangle;
		QPainterPath path1(QPoint(0-markerScale/2+side/2,0));//starting point
		path1.lineTo( markerScale-markerScale/2+side/2, 0);//topleft to topright
		path1.lineTo(markerScale/2-markerScale/2+side/2, markerScale);//
		path1.closeSubpath ();

		QPainterPath path2(QPoint(0-markerScale/2+side/2, side));//starting point
		path2.lineTo( markerScale-markerScale/2+side/2, side);//
		path2.lineTo(markerScale/2-markerScale/2+side/2, side-markerScale );//
		path2.closeSubpath ();
		path1.addPath(path2);


		painter.setPen(QPen(m_markerColor, 1, Qt::SolidLine));
		painter.setBrush ( m_markerColor );

//		painter.drawPath(path1);

		painter.translate(side/2 , side/2);//to center
		painter.rotate(m_angle);//around the centre
		painter.translate(-side/2 , -side/2);//and back

		painter.drawPath(path1);

	}

//	QWidget::paintEvent(event);

  /**/
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
///sets a mask around the dial
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void MiriamDial01::resizeEvent(QResizeEvent * event)
{
	int side = qMin(width(), height());

	QRegion mask( width() / 2 - side / 2, 
										height() / 2 - side / 2, 
										side, side, 
										QRegion::Ellipse);//( int x, int y, int w, int h, RegionType t = Rectangle )
	setMask(mask);

//	setUpdatesEnabled(false);
/**/
}

/*!
    \fn MiriamDial01::backgroundColor() const
 */
const QColor & MiriamDial01::backgroundColor() const
{
    return m_backgroundColor;
}


/*!
    \fn MiriamDial01::middleColor() const
 */
const QColor & MiriamDial01::middleColor() const
{
    return m_middleColor;
}


/*!
    \fn MiriamDial01::setBackgroundColor(QColor color)
 */
void MiriamDial01::setBackgroundColor(QColor color)
{
    m_backgroundColor = color;
    this->update();
}


/*!
    \fn MiriamDial01::setMiddleColor(QColor color)
 */
void MiriamDial01::setMiddleColor(QColor color)
{
    m_middleColor = color;
    this->update();
}


/*!
    \fn MiriamDial01::markerColor() const
 */
const QColor & MiriamDial01::markerColor() const
{
    return m_markerColor;
}


/*!
    \fn MiriamDial01::setMarkerColor(QColor color)
 */
void MiriamDial01::setMarkerColor(QColor color)
{
    m_markerColor = color;
}
